package com.vma.shoeSystem.controller;

import com.jfinal.core.Controller;
import com.vma.shoeSystem.util.ReturnsUtil;
import com.vma.shoes.bizModel.model.Article;
import com.vma.suggestion.service.ArticleService;

import java.util.Date;

/**
 * Created by chenzui on 2017/5/6.
 */
public class ArticleController extends Controller {
    public void qryArticleList(){
        try {
            renderJson(ReturnsUtil.returnSuccess(ArticleService.qryArticles()));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    public void edit(){
        try {
            Integer articleId = getParaToInt("id");
            Article article = Article.dao.findById(articleId);
            setAttr("article",article);
            renderVelocity("/shop/article/editArticle.html");
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    public void updateArticle(){
        try{
            Integer articleId = getParaToInt("id");
            String title = getPara("title");
            String desc = getPara("desc");
            String imgUrl = getPara("imgUrl");
            String content = getPara("content");
            Article article = Article.dao.findById(articleId);
            article.setSimpleDesc(desc).setArticleName(title).setContent(content).setImgUrl(imgUrl).update();
            renderJson(ReturnsUtil.returnSuccess("success"));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    public void updateStatus(){
        try{
            Integer articleId = getParaToInt("id");
            Article article = Article.dao.findById(articleId);
            article.setStatus(article.getStatus().equals("1")?"0":"1").update();
            renderJson(ReturnsUtil.returnSuccess("success"));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    public void delete(){
        try{
            Integer articleId = getParaToInt("id");
            Article article = Article.dao.findById(articleId);
            article.setIfDelete("1").update();
            renderJson(ReturnsUtil.returnSuccess("success"));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    public void addArticle(){
        try{
            String title = getPara("title");
            String desc = getPara("desc");
            String imgUrl = getPara("imgUrl");
            String content = getPara("content");
            new Article().setIfDelete("0").setSimpleDesc(desc).setStatus("1").setCreateTime(new Date())
                    .setArticleName(title).setContent(content).setImgUrl(imgUrl).save();
            renderJson(ReturnsUtil.returnSuccess("success"));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    public void addInit(){
        renderVelocity("/shop/article/article.html");
    }
}
